package com.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.product.enmu.DeletedEnum;
import com.product.enmu.OperatorType;
import com.product.entity.Users;
import com.product.mapper.UsersMapper;
import com.product.service.IOperationLogService;
import com.product.service.IUsersService;
import com.product.to.AddUserTo;
import com.product.to.UpdateUserTo;
import com.product.utils.PageUtils;
import com.product.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {
    @Autowired
    private IOperationLogService operatorService;

    @Override
    public PageUtils getAllUser(Integer size, Integer current, String username) {
        Page<Users> usersPage = new Page<>(current, size);
        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(username != null, Users::getUsername, username);

        Page<Users> page = this.baseMapper.selectPage(usersPage, queryWrapper);

        List<UserVo> userVoList = page.getRecords().stream().map(item -> {
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(item, userVo);
            return userVo;
        }).toList();
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(userVoList);
        return pageUtils;
    }

    @Override
    public void addUser(AddUserTo user) {
        Users users = new Users();
        BeanUtils.copyProperties(user, users);
        this.baseMapper.insert(users);

        operatorService.addOperation(OperatorType.Insert.getCode(), "添加User表");
    }

    @Override
    public void updateUser(UpdateUserTo user) {
        Users users = new Users();
        BeanUtils.copyProperties(user, users);
        this.baseMapper.updateById(users);

        operatorService.addOperation(OperatorType.Update.getCode(), "修改User表");
    }

    @Override
    public void deleteUser(Integer userId) {
        LambdaUpdateWrapper<Users> updateWrapper = new LambdaUpdateWrapper<Users>()
                .eq(Users::getUserId, userId)
                .set(Users::getDeleted, DeletedEnum.DELETE.getValue());
        this.baseMapper.update(updateWrapper);

        operatorService.addOperation(OperatorType.Delete.getCode(), "删除User表");
    }

    @Override
    public void deleteUsers(List<Integer> userIds) {
        for (Integer userId : userIds) {
            deleteUser(userId);
        }
    }

    @Override
    public Users getUserByUsername(String username) {
        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Users::getUsername, username);
        return this.baseMapper.selectOne(queryWrapper);
    }

}
